#include "QtStra_Thread.h"



QtStraThread::QtStraThread(CStrategySys* pStrategy_sys, QObject *parent) : QThread(parent)
{
	m_pFrame = new CFrameEvent( pStrategy_sys );

	if (m_pFrame)
	{

		//start();

	}
	else
	{
		pantheios::log_ERROR("[QtStraThread] m_pFrame init failed");
		m_pFrame = NULL;
	}
		

}

QtStraThread::~QtStraThread()
{
	if (m_pFrame)
	{
		//send terminal sema
		{
			FrameCom_Data framecomData;
			framecomData.type = FrameCom_Type::terminatetype;
			framecomData.varsize = 0;
			framecomData.pvardata = NULL;

			m_pFrame->ReleaseFrameSemaAndSetData(&framecomData);
		}



		wait();


		{
			delete m_pFrame;
			m_pFrame = NULL;
		}
	}


	
	

}

void QtStraThread::Start()
{
	if (m_pFrame)
	{

		start();

	}
}


void QtStraThread::run()
{

	if (m_pFrame)
	{
		while (!m_pFrame->m_bInvalid)
		{
			m_pFrame->Frame();
			//if (gbTerm)
			//	break;
		}
	}

	
}


CFrameEvent* QtStraThread::FrameEvent()
{
	return m_pFrame;

}

CStrategySys* QtStraThread::StrategySys()
{
	return m_pFrame->StrategySys();
}

void QtStraThread::SendMFCData(vector<MFCData*> &vpmfcdata)
{
	if (m_pFrame)
	{
	

		//send del/add info
		typedef vector<MFCData*>::iterator it_vpmfcdata;

		for (it_vpmfcdata itpmfc = vpmfcdata.begin(); itpmfc != vpmfcdata.end() ; itpmfc++)
		{
			MFCData* &pmfcdata = *itpmfc;


			FrameCom_Data framecomData;
			framecomData.type = FrameCom_Type::mfctype;
			framecomData.varsize = sizeof(MFCData);
			framecomData.pvardata = (char*)(pmfcdata);


			m_pFrame->ReleaseFrameSemaAndSetData(&framecomData);
		}

		
	}
	
}


void QtStraThread::SendTimerData(TimerFire_namespace::TimerFireType timertype)
{

	if (m_pFrame)
	{
		TimerData data;
		data.type = timertype;

		FrameCom_Data framecomData;
		framecomData.type = FrameCom_Type::timerfiretype;
		framecomData.varsize = sizeof(TimerData);
		framecomData.pvardata = (char*)(&data);



		m_pFrame->ReleaseFrameSemaAndSetData(&framecomData);
	}


}